#include <nxos.h>
#include <PowerBox.h>

NX_IMPORT NX_Error PowerBoxBuiltinCd(int argc, char *argv[], char * env);
NX_IMPORT NX_Error PowerBoxBuiltinPwd(int argc, char *argv[], char * env);
NX_IMPORT NX_Error PowerBoxBuiltinLs(int argc, char *argv[], char * env);
NX_IMPORT NX_Error PowerBoxBuiltinPs(int argc, char *argv[], char * env);
NX_IMPORT NX_Error PowerBoxBuiltinSleep(int argc, char *argv[], char * env);
NX_IMPORT NX_Error PowerBoxBuiltinEnv(int argc, char *argv[], char * env);
NX_IMPORT NX_Error PowerBoxBuiltinEcho(int argc, char *argv[], char * env);
NX_IMPORT NX_Error PowerBoxBuiltinHello(int argc, char *argv[], char * env);
NX_IMPORT NX_Error PowerBoxBuiltinMonitor(int argc, char *argv[], char * env);
NX_IMPORT NX_Error PowerBoxBuiltinTime(int argc, char *argv[], char * env);
NX_IMPORT NX_Error PowerBoxBuiltinSignal(int argc, char *argv[], char * env);
NX_IMPORT NX_Error PowerBoxBuiltinStart(int argc, char *argv[], char * env);
NX_IMPORT NX_Error PowerBoxBuiltinHelp(int argc, char *argv[], char * env);
NX_IMPORT NX_Error PowerBoxBuiltinExit(int argc, char *argv[], char * env);
NX_IMPORT NX_Error PowerBoxBuiltinHistory(int argc, char *argv[], char * env);
NX_IMPORT NX_Error PowerBoxBuiltinCat(int argc, char *argv[], char * env);

PowerBoxCmd powerBoxCmdTable[] = 
{
    {"help", "print help info", PowerBoxBuiltinHelp},
    {"cd", "change directory", PowerBoxBuiltinCd},
    {"pwd", "print word direcotry", PowerBoxBuiltinPwd},
    {"ls", "list files", PowerBoxBuiltinLs},
    {"ps", "print state", PowerBoxBuiltinPs},
    {"sleep", "sleep for a while", PowerBoxBuiltinSleep},
    {"env", "print environment", PowerBoxBuiltinEnv},
    {"echo", "echo string", PowerBoxBuiltinEcho},
    {"hello", "print hello", PowerBoxBuiltinHello},
    {"monitor", "pirnt monitor info", PowerBoxBuiltinMonitor},
    {"time", "print current time", PowerBoxBuiltinTime},
    {"signal", "send signal to process", PowerBoxBuiltinSignal},
    {"start", "start a new application", PowerBoxBuiltinStart},
    {"exit", "exit powerbox", PowerBoxBuiltinExit},
    {"history", "show history cmd", PowerBoxBuiltinHistory},
    {"cat", "print one file", PowerBoxBuiltinCat},
    {NX_NULL, NX_NULL, NX_NULL},
};

NX_PRIVATE void RunCmd(PowerBoxCmd *cmdInfo, char * cmd, char * env)
{
    char cmdBuf[NX_FILE_MAX_PATH + 1] = {0};
    NX_MemCopy(cmdBuf, cmd, PWBOX_CMD_BUF_LEN);
    char *array[PWBOX_MAX_ARGS + 1];
    int argc;

    argc = NX_CmdToArray(cmdBuf, array, PWBOX_MAX_ARGS);

    cmdInfo->handler(argc, array, env);
}

NX_Error PowerBoxRunBuiltinCmd(char * appName, char * cmd, char * env)
{
    PowerBoxCmd *cmdInfo;

    for (cmdInfo = &powerBoxCmdTable[0]; cmdInfo->name != NX_NULL; cmdInfo++)
    {
        if (!NX_StrCmp(appName, cmdInfo->name))
        {
            RunCmd(cmdInfo, cmd, env);
            return NX_EOK;
        }
    }
    return NX_ENOSRCH;
}

NX_Error PowerBoxSetCwd(char *buf)
{
    return NX_ProcessSetCwd(buf);
}

NX_Error PowerBoxGetCwd(char *buf)
{
    return NX_ProcessGetCwd(buf, NX_FILE_MAX_PATH);
}

NX_Error PowerBoxPrintHelp(void)
{
    PowerBoxCmd *cmdInfo;

    NX_Printf("==== Built-in commands of PowerBox ====\n");
    for (cmdInfo = &powerBoxCmdTable[0]; cmdInfo->name != NX_NULL; cmdInfo++)
    {
        NX_Printf("%-16s - %s\n", cmdInfo->name, cmdInfo->info);
    }
    return NX_EOK;
}
